package com.mt.controller;


import com.mt.constants.Result;
import com.mt.pojo.CommentLike;
import com.mt.service.CommentLikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 *
 * </p>
 *
 * @author Wang
 * @since 2020-11-12
 */
@RestController
@RequestMapping("/like")
@Slf4j
public class CommentLikeController {
    @Autowired
    CommentLikeService likeService;

    public Result addLikeList(List<CommentLike> list) {
        log.debug(list.toString());
        Result<Object> result = new Result<>();
        if (likeService.addLikeList(list)){
            result.success();
        }
        return result;
    }

    public Result modifyLikeList(List<CommentLike> list) {
        log.debug(list.toString());
        Result<Object> result = new Result<>();
        if (likeService.modifyLikeList(list)){
            result.success();
        }
        return result;
    }

    public Result saveLikeList2Db(List<CommentLike> list) {
        log.debug(list.toString());
        Result<Object> result = new Result<>();
        if (likeService.saveLikeList2Db(list)){
            result.success();
        }
        return result;
    }

    public Result countLikeTotal(Map<Integer, Integer> likeMap) {
        log.debug(likeMap.toString());
        Result<Object> result = new Result<>();
        if (likeService.countLikeTotal(likeMap)){
            result.success();
        }
        return result;
    }

    @PostMapping("/add")
    public Result addLike2Redis(@RequestBody CommentLike like,@RequestParam("userId") String userId) {
        like.setUserId(userId);
        log.debug("传入参数:userId:{} , commentId:{} , likeStatus:{}",like.getUserId(),like.getCommentId(),like.getLikeStatus());
        Result<Object> result = new Result<>();
        if (likeService.addLike2Redis(like.getUserId(),like.getCommentId(),like.getLikeStatus())){
            result.success();
        }
        return result;
    }

    public Result getCommentLikeStatus(String userId, int commentId) {
        log.debug("传入参数:userId:{} , commentId:{}",userId,commentId);
        Result<Object> result = new Result<>();
        int commentLikeStatus = likeService.getCommentLikeStatus(userId, commentId);
        result.success(commentLikeStatus);
        return result;
    }
}
